<?php


namespace App\Http\Controllers\Admin;


use App\Model\Rules;
use App\Model\RulesGroup;
use App\Tool\Json;
use Illuminate\Validation\Rule;

class RuleGroupController extends AuthCheckController
{

    /**
     * Create by Peter
     * 2019/08/26 16:18:37
     * Email:904801074@qq.com
     * @return false|string
     */
    function group_list(){

        $list=RulesGroup::orderBy('id');

        return Json::code(1,'success',paginate($list,request()->input('p',1)));
    }


    /**
     * Create by Peter
     * 2019/08/26 16:18:32
     * Email:904801074@qq.com
     * @return false|string
     */
    function all_rules(){

        $list=Rules::all();

        if($list) $list->toArray();

        $list=groupByKey($list,'group_name');

        return Json::code(1,'success',$list);

    }

    /**
     * Create by Peter
     * 2019/08/26 16:18:20
     * Email:904801074@qq.com
     * @return false|string
     */
    function rule_update(){

        $post=request()->post();

        $id=$post['id']??null;

        RulesGroup::updateOrCreate(['id'=>$id],$post);

        return Json::code(1,'success');

    }

    function get_rule_info(int $id){


        $info=RulesGroup::find($id);

        return Json::code(1,'success',$info);
    }

    function group_delete(int $id){

        if(!$id) return Json::code(2,'id不存在！');

        RulesGroup::destroy($id);

        return Json::code(1,'success');
    }

}
